package com.hitqz.robot.driver.util;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import java.util.Optional;
import java.util.concurrent.TimeUnit;

public class GfCacheUtil {

    public static final String KEY_ROBOT_STATUS = "robotStatus";
    public static final String KEY_RELAY_STATUS = "relayStatus";

    private static final Cache<String, Boolean> speedFlagCache = CacheBuilder.newBuilder()
            .maximumSize(1)
            .expireAfterWrite(600, TimeUnit.MILLISECONDS) // 缓存过期时间
            .build();

    private static final Cache<String, Object> cache = CacheBuilder.newBuilder()
            .maximumSize(50)// fixme 缓存最大容量,按key实际种类去扩容
            .expireAfterWrite(5, TimeUnit.SECONDS) // 缓存过期时间
            .build();

    private static final Cache<String, Object> longtimeCache = CacheBuilder.newBuilder()
            .maximumSize(1)
            .expireAfterWrite(61, TimeUnit.SECONDS) // 缓存过期时间
            .build();

    public static void setSpeedFlag() {
        speedFlagCache.put("speed", true);
    }

    public static boolean getSpeedFlag() {
        return Optional.ofNullable(speedFlagCache.getIfPresent("speed")).orElse(false);
    }

    // 使用命令 号做 key
    public static void put(String key, Object value) {
        cache.put(key, value);
    }

    public static Object get(String key) {
        return cache.getIfPresent(key);
    }

    public static void putLongtimeCache(String key,Object value){
        longtimeCache.put(key, value);
    }

    public static Object getLongtimeCache(String key){
        return longtimeCache.getIfPresent(key);
    }

}
